﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using cpe.cpein.DATA;

namespace cpe.cpein.BUSINESS
{
    public static class BUGroupRole
    {
        /// <summary>
        /// Get All the GroupRole of the database.
        /// </summary>
        /// <param name="searchInDeleted"></param>
        /// <returns></returns>
        public static List<GroupRole> GetAllGroupRole(bool searchInDeleted)
        {
            List<GroupRole> GroupRoles = new List<GroupRole>();
            using (cpeinEntities db = new cpeinEntities())
            {
                var results = from GroupRole in db.GroupRole
                              where GroupRole.gpr_isdeleted == searchInDeleted
                              select GroupRole;
                foreach (GroupRole tmp in results)
                    GroupRoles.Add(tmp);
            }

            return GroupRoles;
        }

        /// <summary>
        /// Get the GroupRole by id.
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public static GroupRole GetGroupRoleByID(int id)
        {
            if (id <= 0)
                return null;
            else
            {
                using (cpeinEntities db = new cpeinEntities())
                {
                    var gpr = from GroupRole in db.GroupRole
                              where GroupRole.gpr_id == id
                              select GroupRole;
                    foreach (GroupRole tmp in gpr)
                        return tmp;
                    return null;
                }
            }
        }

        /// <summary>
        /// gpr a GroupRole into the database.
        /// </summary>
        /// <param name="gpr"></param>
        /// <returns></returns>
        public static GroupRole GroupRolerea(GroupRole gpr)
        {
            if (gpr != null)
            {
                using (cpeinEntities db = new cpeinEntities())
                {
                    GroupRole ngpr = db.GroupRole.Add(gpr);
                    db.SaveChanges();
                    return ngpr;
                }
            }
            else
                return null;
        }

        /// <summary>
        /// Remove a specific GroupRole of the database.
        /// </summary>
        /// <param name="gpr"></param>
        /// <returns></returns>
        public static bool RemoveGroupRole(GroupRole gpr)
        {
            if (gpr != null)
            {
                gpr.gpr_isdeleted = true;
                if (UpdateGroupRole(gpr) != null)
                {
                    using (cpeinEntities db = new cpeinEntities())
                    {
                        db.SaveChanges();
                        return true;
                    }
                }
            }

            return false;
        }

        /// <summary>
        /// Update the GroupRole into the database.
        /// </summary>
        /// <param name="gpr"></param>
        /// <returns></returns>
        public static GroupRole UpdateGroupRole(GroupRole gpr)
        {
            if (gpr != null)
            {
                using (cpeinEntities db = new cpeinEntities())
                {
                    var current = from GroupRole in db.GroupRole
                                  where GroupRole.gpr_id == gpr.gpr_id
                                  select GroupRole;

                    foreach (var tmp in current)
                    {
                        tmp.gpr_label = gpr.gpr_label;
                        tmp.gpr_isdeleted = gpr.gpr_isdeleted;

                        db.SaveChanges();
                    }

                    gpr = current as GroupRole;
                }
            }

            return gpr;
        }
    }
}
